<?php

namespace app\commission;

use app\models\user\UserAccount;
use app\models\user\UserCommission;

/**
 * Class Commission
 * @package app\commission
 */
class Commission
{
    public static $instance;

    /**
     * @var SignIn
     */
    private $signIn;

    /**
     * @var Refill
     */
    private $refill;

    /**
     * @var Order
     */
    private $order;

    /**
     * @var Group
     */
    private $group;

    /**
     * @var Merchant
     */
    private $merchant;

    private function __construct()
    {
        $this->signIn = new SignIn();
        $this->refill = new Refill();
        $this->order = new Order();
        $this->merchant = new Merchant();
        $this->group = new Group();
    }

    public function getSignIn()
    {
        return $this->signIn;
    }

    public function getRefill()
    {
        return $this->refill;
    }

    public function getOrder()
    {
        return $this->order;
    }

    public function getMerchant()
    {
        return $this->merchant;
    }

    public function getGroup()
    {
        return $this->group;
    }

    public static function getInstance() {
        if (!self::$instance) {
            self::$instance = new self();
        }

        return self::$instance;
    }

    /**
     * save commission to account
     *
     * @param UserCommission $commission
     */
    public function commissionSaveAccount(UserCommission $commission)
    {
        $account = UserAccount::findOne(['user_id' => $commission->user_id]);

        if ($account) {
            if ($commission->balance) {
                $account->balance += $commission->balance;
            }

            if ($commission->score) {
                $account->score += $commission->score;
            }

            if ($commission->ticket) {
                $account->ticket += $commission->ticket;
            }

            $account->save();
        }

        // todo send message to rabbitmq
        // todo create wx message or mobile message
    }
}